import java.util.*;

class Node {
	int row;
	int column;
	ArrayList<Node> neighbours;
	
	public Node(int r, int c) {
		row = r;
		column = c;
	}
}

public class AdjList {
	ArrayList<Node> list = new ArrayList<Node>();
	
	public boolean insert(int row, int column) {
		if (doesExist(row, column))
			return false;
		
		Node node = new Node(row, column);
		list.add(node);
		
		return true;
	}
	
	public ArrayList<Node> neighbourOf(int row, int col) {
		int i=0;
		while (i < list.size()) {
			if (list.get(i).column == col && list.get(i).row == row)
				return list.get(i).neighbours;
			i++;
		}
		return null;
	}
	
	public boolean doesExist(int row, int col) {
		int i=0;
		while (i < list.size()) {
			if (list.get(i).column == col && list.get(i).row == row)
				return true;
			i++;
		}
		return false;
	}
	
	public boolean insertNeighbour(int row1, int col1, int row2, int col2) {
		int i=0;
		while (i < list.size()) {
			if (list.get(i).column == col1 && list.get(i).row == row1) {
				Node node = new Node(row2, col2);
				list.get(i).neighbours.add(node);
				return true;
			}
			i++;
		}
		
		return false;
	}
	
	public int numEdges() {
		int num = 0;
		int i = 0;
		
		while (i<list.size()){
			num += list.get(i).neighbours.size();
			i++;
		}
		
		return num;
	}
	
}